/**
 * Copyright (C) 2007-2009 Alexis Kinsella - http://www.helyx.org - <Helyx.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.helyx.gae.getabike.xml;

import org.dom4j.Element;
import org.helyx.gae.getabike.constant.BooleanConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ElementValueAccessor {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ElementValueAccessor.class);
	
	private Element elt;
	
	public ElementValueAccessor() {
		super();
	}
	
	public void selectElement(Element elt) {
		this.elt = elt;
	}
	
	public boolean eltExists(String eltName) {
		return elt.element(eltName) != null;
	}
	
	public boolean getEltValueAsBoolean(String eltName) {
		return getEltValueAsBoolean(eltName, false);
	}
	
	public boolean getEltValueAsBoolean(String eltName, boolean defaultValue) {
		String booleanValue = (String)elt.element(eltName).getTextTrim();
		
		if (booleanValue == null || booleanValue.length() == 0) {
			return defaultValue;
		}
		return BooleanConstants.TRUE.compareTo(booleanValue.toLowerCase()) == 0;
	}
	
	public byte getEltValueAsByte(String eltName) {
		String byteValue = (String)elt.element(eltName).getTextTrim();
		
		if (byteValue == null || byteValue.length() == 0) {
			return 0;
		}
		return Byte.parseByte(byteValue);
	}
	
	public short getEltValueAsShort(String eltName) {
		String shortValue = (String)elt.element(eltName).getTextTrim();
		
		if (shortValue == null || shortValue.length() == 0) {
			return 0;
		}
		return Short.parseShort(shortValue);
	}
	
	public int getEltValueAsInt(String eltName) {
		String intValue = (String)elt.element(eltName).getTextTrim();
		
		if (intValue == null || intValue.length() == 0) {
			return 0;
		}
		return Integer.parseInt(intValue);
	}
	
	public long getEltValueAsLong(String eltName) {
		String longValue = (String)elt.element(eltName).getTextTrim();
		
		if (longValue == null || longValue.length() == 0) {
			return 0;
		}
		return Long.parseLong(longValue);
	}

	public float getEltValueAsFloat(String eltName) {
		String floatValue = (String)elt.element(eltName).getTextTrim();
		
		if (floatValue == null || floatValue.length() == 0) {
			return 0;
		}
		return Float.parseFloat(floatValue);
	}
	
	public double getEltValueAsDouble(String eltName) {
		String doubleValue = (String)elt.element(eltName).getTextTrim();
		
		if (doubleValue == null || doubleValue.length() == 0) {
			return 0;
		}
		return Double.parseDouble((String)elt.element(eltName).getTextTrim());
	}
	
	public String getEltValueAsString(String eltName) {
		return getEltValueAsString(eltName, null);
	}
	
	public String getEltValueAsString(String eltName, String defaultValue) {
		String value = (String)elt.element(eltName).getTextTrim();
		if (value != null) {
			return value;
		}
		
		return defaultValue;
	}

}
